/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package lifecraft;

import java.io.FileInputStream;
import java.io.IOException;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;





final class SurfaceBase {
    private static SurfaceBase Base = new SurfaceBase();
    
    
     
    public static SurfaceBase get(){
        return Base;
    }
    public Surface getFromID(SurfC ID){
        switch(ID){
            case DIRT:
                return dirt;
            case STONE:
                return stone;
            case WATER:
                return water;
            case GRASS:
                return grass;
            case KILOF:
                return kilof;
        }
        return grass;
    }
    public Surface dirt;
    public Surface stone;
    public Surface water;
    public Surface grass;
    public Surface kilof;
    private SurfaceBase(){        
    }
    public void init() {
        
        System.out.println("Inicjalizacja grafiki");
        dirt= new Surface(); dirt.loadFromID(SurfC.DIRT);
        water= new Surface(); water.loadFromID(SurfC.WATER);
        grass= new Surface();grass.loadFromID(SurfC.GRASS);
        kilof= new Surface(); kilof.loadFromID(SurfC.KILOF);
        stone= new Surface(); stone.loadFromID(SurfC.STONE);
    }
}


class Surface {
    private Texture txt;
    public void load(String Type, String FileName) {
        try {
            txt=TextureLoader.getTexture(Type, new FileInputStream(FileName),true);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    Texture GetTxt()
    {
        return txt;
    }
    
    public void loadFromID(SurfC ID) {
        
        String FileName="images/";
        
        switch(ID){
            case DIRT:
                FileName+="dirt.png";
                break;
            case STONE:
                FileName+="stone.png";
                break;
            case WATER:
                FileName+="water.png";
                break;
            case GRASS:
                FileName+="grass.png";
                break;
            case KILOF:
                FileName+="kilof.png";
                break;
                
        }
        
        System.out.println("Próbuje ładować "+FileName);
        load("PNG", FileName);
    }
    public void use(int x, int y, int w, int h) {
        GL11.glColor3f(1.f, 1.f, 1.f);
            
            txt.bind();
            GL11.glBegin(GL11.GL_QUADS);
                GL11.glTexCoord2f(0.0f, 0.0f);
                GL11.glVertex2f(x, y);
                GL11.glTexCoord2f(1f, 0.0f);
                GL11.glVertex2f(x+w, y);
                GL11.glTexCoord2f(1f, 1f);
                GL11.glVertex2f( x+w, y+h);	
                GL11.glTexCoord2f(0.0f, 1f);
                GL11.glVertex2f(x, y+h);	
            
            GL11.glEnd();
    }
    public void useLight(int x, int y, int w, int h,float txth, float txtw,float LightLevel) {
        
        
        GL11.glColor3f(LightLevel-0.032f, LightLevel-0.032f, LightLevel-0.032f);
            
            txt.bind();
            GL11.glBegin(GL11.GL_QUADS);
                GL11.glTexCoord2f(txtw, txth);
                GL11.glVertex2f(x, y);
                GL11.glTexCoord2f(txtw+0.25f, txth);
                GL11.glVertex2f(x+w, y);
                GL11.glColor3f(LightLevel, LightLevel, LightLevel);
                GL11.glTexCoord2f(txtw+0.25f, txth+0.25f);
                GL11.glVertex2f( x+w, y+h);	
                GL11.glTexCoord2f(txtw, txth+0.25f);
                GL11.glVertex2f(x, y+h);	
            
            GL11.glEnd();
    }
    public void use(int x, int y, int w, float degree) {
        double radian = Math.toRadians(degree);
        double d = w*Math.sqrt(2);
        
        GL11.glColor3f(1.f, 1.f, 1.f);
            
            txt.bind();
            GL11.glBegin(GL11.GL_QUADS);
                GL11.glTexCoord2f(0.0f, 0.0f);
                GL11.glVertex2d(x, y);
                GL11.glTexCoord2f(1f, 0.0f);
                GL11.glVertex2d(x + w*Math.cos(radian), y - w*Math.sin(radian));
                GL11.glTexCoord2f(1f, 1f);
                GL11.glVertex2d( x + d*Math.cos(radian+Math.toRadians(45f)), y - d*Math.sin(radian+Math.toRadians(45f)));	
                GL11.glTexCoord2f(0.0f, 1f);
                GL11.glVertex2d(x - w*Math.cos(Math.toRadians(90f)-radian), y - w*Math.sin(Math.toRadians(90f)-radian));	
            
            GL11.glEnd();
 
    }

}

